package com.ems.sys.service.impl;

import com.ems.sys.dao.RoleMapper;
import com.ems.sys.dao.UserMapper;
import com.ems.sys.pojo.Users;
import com.ems.sys.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/****
 * @Author:传智播客
 * @Description:Emlpoyee业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * Emlpoyee条件+分页查询
     * @param users 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Users> findPage(Users users, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(users);
        //执行搜索
        List<Users> users1 = userMapper.selectByExample(example);
        for (int i=0;i<users1.size();i++) {
            users1.get(i).setRoleList(roleMapper.findRoleByUserId(users1.get(i).getuId()));
        }

        return new PageInfo<Users>(users1);
    }

    /**
     * Emlpoyee分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Users> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Users>(userMapper.findByUsername());
    }

    /**
     * Emlpoyee条件查询
     * @param users
     * @return
     */
    @Override
    public List<Users> findList(Users users){
        //构建查询条件
        Example example = createExample(users);
        //根据构建的条件查询数据
        return userMapper.selectByExample(example);
    }

    /**
     * Emlpoyee构建查询对象
     * @param users
     * @return
     */
    public Example createExample(Users users){
        Example example=new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        if(users!=null){
            // 工员id
            if(!StringUtils.isEmpty(users.getuId())){
                    criteria.andEqualTo("uId",users.getuId());
            }
            // 部门编号
            if(!StringUtils.isEmpty(users.getUsername())){
                    criteria.andEqualTo("username",users.getUsername());
            }
            // 职位编号
            if(!StringUtils.isEmpty(users.getPassword())){
                    criteria.andEqualTo("password",users.getPassword());
            }
            // 员工名称
            if(!StringUtils.isEmpty(users.getCreateDate())){
                    criteria.andEqualTo("createDate",users.getCreateDate());
            }
            // 员工名称
            if(!StringUtils.isEmpty(users.getStatus())){
                criteria.andEqualTo("status",users.getStatus());
            }
// 员工名称
            if(!StringUtils.isEmpty(users.getImgUrl())){
                criteria.andEqualTo("imgUrl",users.getImgUrl());
            }
            // 员工名称
            if(!StringUtils.isEmpty(users.getVerifyCode())){
                criteria.andEqualTo("verifyCode",users.getVerifyCode());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Emlpoyee
     * @param emlpoyee
     */
    @Override
    public void update(Users emlpoyee){
        userMapper.updateByPrimaryKey(emlpoyee);
    }

    /**
     * 增加users
     * @param users
     */
    @Override
    public void add(Users users){
        users.setPassword(new BCryptPasswordEncoder().encode(users.getPassword()));
        userMapper.insert(users);
    }

    @Override
    public List<Users> findByUsername() {
        return userMapper.findByUsername();
    }

    /**
     * 根据ID查询Emlpoyee
     * @param id
     * @return
     */
    @Override
    public Users findById(String id){
        return  userMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Emlpoyee全部数据
     * @return
     */
    @Override
    public List<Users> findAll() {
        return userMapper.selectAll();
    }



}
